package org.donhuan.rms.ui.editors;

import java.util.List;

import org.donghuan.rms.framework.domain.Archive;
import org.donghuan.rms.framework.domain.RecordFile;
import org.donhuan.rms.ui.ImageCache;
import org.donhuan.rms.ui.ImageKeys;
import org.donhuan.rms.ui.actions.RmsTableViewActionGroup;
import org.donhuan.rms.ui.model.DataFactory;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.wb.swt.SWTResourceManager;

public class ArchiveEditor extends EditorPart {
	private DataBindingContext m_bindingContext;

	public static final String ID = "org.donhuan.rms.ui.editors.ArchiveEditor"; //$NON-NLS-1$
	private DataFactory data=DataFactory.getInstance();
	private Text text_label;
	private Text text_isbn;
	private Text text_2;
	private Table table;
	private ExpandBar expandBar;
	private Table table_1;
	private RmsTableViewActionGroup actionGroup;
	private Archive archive;
	private TableViewer tableViewer_recordFiles;
	private Combo combo_destination;
	private Combo combo_founder;
	private Combo combo_agenter;
	private Combo combo_suspecter;
	private DateTime dateTime_createDate;
	private DateTime dateTime_incidentDate;
	private CCombo combo_1;
	public ArchiveEditor() {
	}

	/**
	 * Create contents of the editor part.
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(1, false));
		
		expandBar = new ExpandBar(container, SWT.V_SCROLL);
		expandBar.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.BOLD));
		expandBar.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		expandBar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		ExpandItem Expanditem_archiveDetail = new ExpandItem(expandBar, SWT.NONE);
		Expanditem_archiveDetail.setImage(ImageCache.getInstance().getImage(ImageKeys.OBJ_ARCHIVE));
		Expanditem_archiveDetail.setExpanded(true);
		Expanditem_archiveDetail.setText("\u6863\u6848\u57FA\u672C\u4FE1\u606F");
		
		final Composite composite_detail = new Composite(expandBar, SWT.NONE);
		Expanditem_archiveDetail.setControl(composite_detail);
		composite_detail.setLayout(new GridLayout(2, false));
		
		Composite composite_2 = new Composite(composite_detail, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite_2.setLayout(new GridLayout(4, false));
		
		CLabel lblNewLabel = new CLabel(composite_2, SWT.NONE);
		lblNewLabel.setText("\u6863\u6848\u540D\u79F0");
		
		text_label = new Text(composite_2, SWT.BORDER);
		text_label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1));
		
		CLabel label = new CLabel(composite_2, SWT.NONE);
		label.setText("\u6863\u6848\u7F16\u53F7");
		
		text_isbn = new Text(composite_2, SWT.BORDER);
		text_isbn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		CLabel label_4 = new CLabel(composite_2, SWT.NONE);
		label_4.setText("\u6863\u6848\u53BB\u5411");
		
		combo_destination = new Combo(composite_2, SWT.NONE);
		combo_destination.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		
		CLabel label_5 = new CLabel(composite_2, SWT.NONE);
		label_5.setText("\u6863\u6848\u5206\u7C7B");
		
		Combo combo_category_1 = new Combo(composite_2, SWT.NONE);
		combo_category_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
		
		Combo combo_category_2 = new Combo(composite_2, SWT.NONE);
		combo_category_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		CLabel label_2 = new CLabel(composite_2, SWT.NONE);
		label_2.setText("\u5ACC\u7591\u5BF9\u8C61");
		
		text_2 = new Text(composite_2, SWT.BORDER);
		text_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
		
		combo_suspecter = new Combo(composite_2, SWT.NONE);
		combo_suspecter.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		CLabel label_6 = new CLabel(composite_2, SWT.NONE);
		label_6.setText("\u521B\u5EFA\u4EBA\u5458");
		
		combo_founder = new Combo(composite_2, SWT.NONE);
		combo_founder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		CLabel label_3 = new CLabel(composite_2, SWT.NONE);
		label_3.setText("\u7ECF\u529E\u6C11\u8B66");
		
		combo_agenter = new Combo(composite_2, SWT.NONE);
		combo_agenter.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		CLabel label_7 = new CLabel(composite_2, SWT.NONE);
		label_7.setText("\u521B\u5EFA\u65E5\u671F");
		
		dateTime_createDate = new DateTime(composite_2, SWT.BORDER | SWT.DROP_DOWN | SWT.LONG);
		dateTime_createDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		CLabel label_1 = new CLabel(composite_2, SWT.NONE);
		label_1.setText("\u6848\u53D1\u65E5\u671F");
		
		dateTime_incidentDate = new DateTime(composite_2, SWT.BORDER | SWT.DROP_DOWN | SWT.LONG);
		dateTime_incidentDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		ViewForm viewForm = new ViewForm(composite_detail, SWT.NONE);
		GridData gd_viewForm = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 2);
		gd_viewForm.widthHint = 212;
		viewForm.setLayoutData(gd_viewForm);
		
		tableViewer_recordFiles = new TableViewer(viewForm, SWT.BORDER | SWT.CHECK);
		table = tableViewer_recordFiles.getTable();
		table.setHeaderVisible(true);
		viewForm.setContent(table);
		
		ToolBar toolBar = new ToolBar(viewForm, SWT.FLAT | SWT.RIGHT);
		viewForm.setTopRight(toolBar);
		
		combo_1 = new CCombo(viewForm, SWT.BORDER);
		combo_1.setItems(new String[] {});
		combo_1.addSelectionListener(new SelectionAdapter() {
			@SuppressWarnings("unchecked")
			@Override
			public void widgetSelected(SelectionEvent e) {
				archive.setRecordfiles((List<RecordFile>) DataFactory.getInstance().getRecordFiles(((CCombo)e.getSource()).getText()));
				tableViewer_recordFiles.refresh();
				
			}
		});
		viewForm.setTopLeft(combo_1);
		
		Composite composite_button = new Composite(composite_detail, SWT.NONE);
		composite_button.setLayout(new GridLayout(4, false));
		composite_button.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
		
		Button button_save = new Button(composite_button, SWT.NONE);
		button_save.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.out.println(archive.toString());
			}
		});
		button_save.setImage(ImageCache.getInstance().getImage(ImageKeys.TABLE_SAVE));
		button_save.setText("\u4FDD\u5B58\u4FE1\u606F");
		
		Button button_clear = new Button(composite_button, SWT.NONE);
		button_clear.setImage(ImageCache.getInstance().getImage(ImageKeys.TABLE_CLEAR));
		button_clear.setText("\u91CD\u7F6E\u4FE1\u606F");
		button_clear.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				archive=new Archive();
				composite_detail.update();
			}
		});
		
		Button button_create = new Button(composite_button, SWT.NONE);
		button_create.setImage(ImageCache.getInstance().getImage(ImageKeys.TABLE_EDIT));
		button_create.setText("\u521B\u5EFA\u8D44\u6599");
		
		Button button_auto = new Button(composite_button, SWT.NONE);
		button_auto.setImage(ImageCache.getInstance().getImage(ImageKeys.TABLE_INSERT));
		button_auto.setText("\u751F\u6210\u9875\u7801");
		
		fillActionToActionBar(tableViewer_recordFiles, new ToolBarManager(toolBar));
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(200);
		tableColumn.setText("\u540D\u79F0");
		
		
		Expanditem_archiveDetail.setHeight(250);
		createExpandItem(expandBar);
		createExpandItem(expandBar);

	}
	private void fillActionToActionBar(TableViewer tableViewer,IToolBarManager toolBarManager){
		actionGroup=new RmsTableViewActionGroup(getSite().getWorkbenchWindow(), tableViewer);
		actionGroup.fillLocalToolBar(toolBarManager);
		MenuManager popmenu=new MenuManager("#popupmenu");
		actionGroup.fillContextMenu(popmenu);
	}
	private void createExpandItem(ExpandBar expandBar){
		
		ExpandItem xpndtmNewExpanditem_1 = new ExpandItem(expandBar, SWT.NONE);
		xpndtmNewExpanditem_1.setExpanded(true);
		xpndtmNewExpanditem_1.setText("New ExpandItem");
		
		ViewForm viewForm = new ViewForm(expandBar, SWT.NONE);
		xpndtmNewExpanditem_1.setControl(viewForm);
		
		ToolBar toolBar = new ToolBar(viewForm, SWT.FLAT | SWT.RIGHT);
		viewForm.setTopRight(toolBar);
		
		ToolItem toolItem = new ToolItem(toolBar, SWT.NONE);
		toolItem.setImage(SWTResourceManager.getImage(ArchiveEditor.class, "/icons/full/etool16/prev_nav.gif"));
		
		ToolItem toolItem_1 = new ToolItem(toolBar, SWT.NONE);
		toolItem_1.setImage(SWTResourceManager.getImage(ArchiveEditor.class, "/icons/full/etool16/next_nav.gif"));
		
		ToolItem toolItem_2 = new ToolItem(toolBar, SWT.NONE);
		toolItem_2.setImage(SWTResourceManager.getImage(ArchiveEditor.class, "/icons/full/obj16/includeMode_filter.gif"));
		
		ToolItem toolItem_3 = new ToolItem(toolBar, SWT.NONE);
		toolItem_3.setImage(SWTResourceManager.getImage(ArchiveEditor.class, "/icons/full/obj16/delete_obj.gif"));
		
		ToolItem toolItem_4 = new ToolItem(toolBar, SWT.NONE);
		toolItem_4.setImage(SWTResourceManager.getImage(ArchiveEditor.class, "/icons/full/elcl16/refresh_nav.gif"));
		
		ToolItem toolItem_6 = new ToolItem(toolBar, SWT.NONE);
		toolItem_6.setImage(SWTResourceManager.getImage(ArchiveEditor.class, "/icons/full/etool16/editor_area.gif"));
		
		ToolItem toolItem_5 = new ToolItem(toolBar, SWT.NONE);
		toolItem_5.setImage(SWTResourceManager.getImage(ArchiveEditor.class, "/icons/full/etool16/save_edit.gif"));
		
		CCombo combo = new CCombo(viewForm, SWT.BORDER);
		viewForm.setTopLeft(combo);
		
		TableViewer tableViewer = new TableViewer(viewForm, SWT.BORDER | SWT.FULL_SELECTION);
		table_1 = tableViewer.getTable();
		table_1.setLinesVisible(true);
		table_1.setHeaderVisible(true);
		viewForm.setContent(table_1);
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn = tableViewerColumn.getColumn();
		tableColumn.setWidth(100);
		tableColumn.setText("\u540D\u79F0");
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_1 = tableViewerColumn_1.getColumn();
		tableColumn_1.setWidth(100);
		tableColumn_1.setText("\u7C7B\u522B");
		
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_2 = tableViewerColumn_2.getColumn();
		tableColumn_2.setWidth(100);
		tableColumn_2.setText("\u5B9E\u65BD\u5BF9\u8C61");
		
		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_3 = tableViewerColumn_3.getColumn();
		tableColumn_3.setWidth(100);
		tableColumn_3.setText("\u8D23\u4EFB\u76EE\u6807");
		
		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_4 = tableViewerColumn_4.getColumn();
		tableColumn_4.setWidth(100);
		tableColumn_4.setText("\u7ECF\u529E\u6C11\u8B66");
		
		TableViewerColumn tableViewerColumn_5 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_5 = tableViewerColumn_5.getColumn();
		tableColumn_5.setWidth(100);
		tableColumn_5.setText("\u65E5\u671F");
		
		TableViewerColumn tableViewerColumn_6 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_6 = tableViewerColumn_6.getColumn();
		tableColumn_6.setWidth(40);
		tableColumn_6.setText("\u9875\u6570");
		
		TableViewerColumn tableViewerColumn_7 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_7 = tableViewerColumn_7.getColumn();
		tableColumn_7.setWidth(40);
		tableColumn_7.setText("\u59CB\u9875");
		
		TableViewerColumn tableViewerColumn_8 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tableColumn_8 = tableViewerColumn_8.getColumn();
		tableColumn_8.setWidth(40);
		tableColumn_8.setText("\u7EC8\u9875");
		xpndtmNewExpanditem_1.setHeight(120);
		m_bindingContext = initDataBindings();
		
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// Do the Save operation
	}

	@Override
	public void doSaveAs() {
		// Do the Save As operation
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
		archive=((ArchiveEditorInput)getEditorInput()).getElement();
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = PojoObservables.observeMap(listContentProvider.getKnownElements(), RecordFile.class, "recordFileName");
		tableViewer_recordFiles.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		tableViewer_recordFiles.setContentProvider(listContentProvider);
		//
		IObservableList recordfilesArchiveObserveList = PojoProperties.list("recordfiles").observe(archive);
		tableViewer_recordFiles.setInput(recordfilesArchiveObserveList);
		//
		IObservableValue observeTextText_labelObserveWidget = WidgetProperties.text(SWT.FocusOut).observe(text_label);
		IObservableValue archiveNameArchiveObserveValue = PojoProperties.value("archiveName").observe(archive);
		bindingContext.bindValue(observeTextText_labelObserveWidget, archiveNameArchiveObserveValue, null, null);
		//
		IObservableValue observeTextText_1ObserveWidget = WidgetProperties.text(SWT.FocusOut).observe(text_isbn);
		IObservableValue iSBNArchiveObserveValue = PojoProperties.value("ISBN").observe(archive);
		bindingContext.bindValue(observeTextText_1ObserveWidget, iSBNArchiveObserveValue, null, null);
		//
		IObservableValue observeSelectionCombo_2ObserveWidget = WidgetProperties.selection().observe(combo_destination);
		IObservableValue locationArchiveObserveValue = PojoProperties.value("location").observe(archive);
		bindingContext.bindValue(observeSelectionCombo_2ObserveWidget, locationArchiveObserveValue, null, null);
		//
		IObservableValue observeSelectionCombo_agenterObserveWidget = WidgetProperties.selection().observe(combo_agenter);
		IObservableValue agentertrueNameArchiveObserveValue = PojoProperties.value("agenter.trueName").observe(archive);
		bindingContext.bindValue(observeSelectionCombo_agenterObserveWidget, agentertrueNameArchiveObserveValue, null, null);
		//
		IObservableValue observeSelectionCombo_suspecterObserveWidget = WidgetProperties.selection().observe(combo_suspecter);
		IObservableValue suspectertrueNameArchiveObserveValue = PojoProperties.value("suspecter.trueName").observe(archive);
		bindingContext.bindValue(observeSelectionCombo_suspecterObserveWidget, suspectertrueNameArchiveObserveValue, null, null);
		//
		IObservableValue observeSelectionDateTime_createDateObserveWidget = WidgetProperties.selection().observe(dateTime_createDate);
		IObservableValue createDateArchiveObserveValue = PojoProperties.value("createDate").observe(archive);
		bindingContext.bindValue(observeSelectionDateTime_createDateObserveWidget, createDateArchiveObserveValue, null, null);
		//
		IObservableValue observeSelectionDateTime_incidentDateObserveWidget = WidgetProperties.selection().observe(dateTime_incidentDate);
		IObservableValue incidentDateArchiveObserveValue = PojoProperties.value("incidentDate").observe(archive);
		bindingContext.bindValue(observeSelectionDateTime_incidentDateObserveWidget, incidentDateArchiveObserveValue, null, null);
		//
		IObservableValue observeSelectionCombo_founderObserveWidget = WidgetProperties.selection().observe(combo_founder);
		IObservableValue foundertrueNameArchiveObserveValue = PojoProperties.value("founder.trueName").observe(archive);
		bindingContext.bindValue(observeSelectionCombo_founderObserveWidget, foundertrueNameArchiveObserveValue, null, null);
		//
		IObservableList itemsCombo_1ObserveWidget = WidgetProperties.items().observe(combo_1);
		IObservableList itemsDataObserveList = PojoProperties.list("items").observe(data);
		bindingContext.bindList(itemsCombo_1ObserveWidget, itemsDataObserveList, null, null);
		//
		return bindingContext;
	}
}
